package rjl.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import rjl.domain.User;
import rjl.service.Userservice;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/2/28.
 */
@Controller
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven<User>{
    private Log log = LogFactory.getLog(this.getClass());
    private String message;
    private User model = new User();


    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

   /* public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
*/
    @Resource
    private Userservice userservice;

    public String registerUI(){
        return "registerUI";
    }

    public String register(){
        User user = userservice.findusernamepassword(model.getUsername(),model.getPassword()) ;
        /*放入值栈空间的对象栈中*/
        ActionContext.getContext().getValueStack().push(user);
        if (user != null){
            message = "注册成功";
            addActionError(message);
            addActionMessage(message);
            addFieldError("a1",message);
            List<User> userList = userservice.findAll();
            /*将查找出来的对象放到值栈空间中*/
            ActionContext.getContext().put("userList",userList);

            return "success";
        }
        user = new User(model.getUsername(),model.getPassword());
        ActionContext.getContext().getSession().put("user",user);
        message = "注册失败该用户名已经被注册过了";
        log.debug(message);
        log.error(message);
        addActionError(message);
        addActionMessage(message);
        addFieldError("a1",message);
        return "false";
    }

    @Override
    public User getModel() {
        return model;
    }
}
